package com.ym521.skeleton.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.annotation.IntRange;
import com.ym521.skeleton.R;
import com.ym521.skeleton.widget.ShimmerLayout;

/**
 * @author Ym
 * E-mail: 2435970206@qq.com
 * createTime:2023/8/6
 * explain:
 */
public final class SkeletonBaseAdapter extends BaseAdapter {
    private int mItemCount;
    private int mLayoutReference;
    private int[] mLayoutArrayReferences;
    private int mColor;
    private boolean mShimmer;
    private int mShimmerDuration;
    private int mShimmerAngle;

    @Override
    public int getCount() {
        return mItemCount;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (doesArrayOfLayoutsExist()) {
            mLayoutReference = getItemViewType(position);
        }
        if (mShimmer) {
            convertView = inflater.inflate(R.layout.layout_shimmer, parent, false);
            ShimmerLayout shimmerLayout = (ShimmerLayout) convertView;
            shimmerLayout.setShimmerAnimationDuration(mShimmerDuration);
            shimmerLayout.setShimmerAngle(mShimmerAngle);
            shimmerLayout.setShimmerColor(mColor);
            shimmerLayout.startShimmerAnimation();

            ViewGroup layout = (ViewGroup) convertView;
            View view = inflater.inflate(mLayoutReference, layout, false);
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            if (lp != null) {
                layout.setLayoutParams(lp);
            }
            layout.addView(view);
        } else {
            convertView = inflater.inflate(mLayoutReference, parent, false);
        }
        return convertView;
    }


    @Override
    public int getItemViewType(int position) {
        if (doesArrayOfLayoutsExist()) {
            return getCorrectLayoutItem(position);
        }
        return super.getItemViewType(position);
    }

    public void setLayoutReference(int layoutReference) {
        this.mLayoutReference = layoutReference;
    }

    public void setArrayOfLayoutReferences(int[] layoutReferences) {
        this.mLayoutArrayReferences = layoutReferences;
    }

    public void setItemCount(int itemCount) {
        this.mItemCount = itemCount;
    }

    public void setShimmerColor(int color) {
        this.mColor = color;
    }

    public void shimmer(boolean shimmer) {
        this.mShimmer = shimmer;
    }

    public void setShimmerDuration(int shimmerDuration) {
        this.mShimmerDuration = shimmerDuration;
    }

    public void setShimmerAngle(@IntRange(from = -45, to = 45) int shimmerAngle) {
        this.mShimmerAngle = shimmerAngle;
    }

    public int getCorrectLayoutItem(int position) {
        if (doesArrayOfLayoutsExist()) {
            return mLayoutArrayReferences[position % mLayoutArrayReferences.length];
        }
        return mLayoutReference;
    }

    private boolean doesArrayOfLayoutsExist() {
        return mLayoutArrayReferences != null && mLayoutArrayReferences.length != 0;
    }
}
